<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
require_once BASE_PATH . 'application/controllers/canal' . EXT;

class Videos extends Canal {

    public function __construct() {
        parent::__construct();
        $this->load->model('videoDAO');
        $this->load->model("sessionDAO");
        $this->load->model("suscriptoresDAO");
    }

    public function _filters($data = array()) {

        if (empty($data["order_by"]))
            $order_by = array("publish_up", "DESC");
        else
            $order_by = array($data["order_by"], "DESC");

        $where = array();

        if ($data["catid"] != 0)
            $where = array("catid" => $data["catid"]);

        if ($data["last_days"] != 0)
            $this->db->where('created >= DATE_ADD(NOW(),INTERVAL -' . $data["last_days"] . ' DAY)', NULL, FALSE);

        return $this->videoDAO->select(array("order" => $order_by, "where" => $where, "limit" => $data["limit"]));
    }

    public function index() {

        $order_by = $this->input->get("order_by");
        $last_days = $this->input->get("last_days");
        $catid = $this->input->get("catid");

        $videos = $this->_filters(array(
            "order_by" => $order_by,
            "last_days" => $last_days,
            "catid" => $catid,
            "limit" => array(0, 1000)
                ));


        $paginacion = $this->paginationDAO->getItems(array("paginas" => 16, "total" => count($videos), "url" => "./videos/1"));

        $videos = $this->_filters(array(
            "order_by" => $order_by,
            "last_days" => $last_days,
            "catid" => $catid,
            "limit" => array($paginacion["inicio"], $paginacion["fin"])
                ));


        $categorias = $this->videoDAO->getCategories();

        $estasemana = $this->moduleDAO->getParamsByStringId("home_estasemana");

        /*
          Publicidad
         */
        $publicidadheader2 = $this->adsDAO->getAdsense("videos_publicidadheaderright");
        $publicidadheader1 = $this->adsDAO->getAdsense("videos_publicidadheaderleft");

        $publicidadskyscraper1 = $this->adsDAO->getAdsense("videos_publicidadskyscraper1");
        $publicidadskyscraper2 = $this->adsDAO->getAdsense("videos_publicidadskyscraper2");

        $publicidadmiddle1 = $this->adsDAO->getAdsense("videos_publicidadmiddle1");
        $publicidadmiddle2 = $this->adsDAO->getAdsense("videos_publicidadmiddle2");

        $publicidadfooterleft = $this->adsDAO->getAdsense("videos_publicidadfooterleft");
        $publicidadfooterright = $this->adsDAO->getAdsense("videos_publicidadfooterright");

        $correopass = $this->input->get('item');
        $data = array(
            "header" => array(
                "title" => "Cachondamedia",
                "subtitle" => "",
                "description" => "",
                "correopass" => $correopass,
                "keywords" => "",
                "session" => $this->sessionDAO->getUser(),
                "publicidadheader2" => $publicidadheader2,
                "publicidadheader1" => $publicidadheader1,
            ),
            "categorias" => $categorias,
            "videos" => $videos,
            "paginacion" => $paginacion["paginacion"],
            // Publicidad
            "publicidadskyscraper1" => $publicidadskyscraper1,
            "publicidadskyscraper2" => $publicidadskyscraper2,

            "publicidadmiddle1" => $publicidadmiddle1,
            "publicidadmiddle2" => $publicidadmiddle2,

            "publicidadfooterleft" => $publicidadfooterleft,
            "publicidadfooterright" => $publicidadfooterright,
        );

        $this->load->view('videos/videos', $data);
    }

}